//
//  PopularityListViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/6.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "PopularityListViewController.h"
#import "PopularityTableViewCell.h"
#import "ClassmateDetailViewController.h"

@interface PopularityListViewController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSMutableArray *myPopularityArray;
@end

@implementation PopularityListViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableArray *)myPopularityArray{
    if (!_myPopularityArray) {
        _myPopularityArray = [NSMutableArray array];
    }
    return _myPopularityArray;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    //    [Global setStatusBarBackgroundColor:[UIColor clearColor]];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CGRect rect = self.view.bounds;
    rect.size.height -= 41 + 64;
    self.tableView.frame = rect;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PopularityTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"PopularityTableViewCell"];
    [self creatList];
}
- (void)creatList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppHomePageGetUserOrderScore] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            NSMutableArray *array = [[NSMutableArray alloc]init];
            NSMutableArray *popularityList = [PopularityInfo mj_objectArrayWithKeyValuesArray:response[@"oureList"]];
            if (self.popularityListType == KPopularityListDayType) {
                array = [NSMutableArray arrayWithArray:response[@"dayList"]];
                [self.myPopularityArray addObject:popularityList[0]];
            }else if (self.popularityListType == KPopularityListWeekType){
                 array = [NSMutableArray arrayWithArray:response[@"weekList"]];
                [self.myPopularityArray addObject:popularityList[1]];
            }else if(self.popularityListType == KPopularityListMonthType){
                array = [NSMutableArray arrayWithArray:response[@"monthList"]];
                [self.myPopularityArray addObject:popularityList[2]];
            }
//            [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                NSMutableDictionary *dic= [[NSMutableDictionary alloc]initWithDictionary:obj];
//                [dic setValue:[NSString stringWithFormat:@"%lu",(unsigned long)idx+1] forKey:@"num"];
//                [array replaceObjectAtIndex:idx withObject:dic];
//                
//            }];
            [self.dataArray addObjectsFromArray:[PopularityInfo mj_objectArrayWithKeyValuesArray:array]];
            
            [self.tableView reloadData];
        }
    }];
}

- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section) {
        return self.dataArray.count;
    }
    return self.myPopularityArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    PopularityTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"PopularityTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    __weak typeof(self) unself = self;
    cell.enterImageViewBlock = ^(PopularityInfo *popularityInfo) {
        ClassmateDetailViewController *classmateDetailVC = [[ClassmateDetailViewController alloc]init];
        classmateDetailVC.perentType = @"popularity";
        classmateDetailVC.popularityInfo = unself.dataArray[indexPath.row];
        [unself.navigationController pushViewController:classmateDetailVC animated:YES];
    };
    if (indexPath.section) {
        cell.popularityInfo = self.dataArray[indexPath.row];
    }else{
        cell.popularityInfo = self.myPopularityArray[indexPath.row];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 82;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectZero;
    if (section) {
        view.frame = CGRectMake(0, 0, 0, 10);
    }
    view.backgroundColor = KBACKGROUNDCOLOR;
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section) {
        return 10.01f;
    }
    return 0.01f;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
